﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


//Most of this is direct copy from day details procedure. A lot of this will be in service layer/stored procedures eventually

namespace CalendarApplication
{
    public partial class frmAddAppointment : Form
    {
        //TODO will need to also capture "user" and "client" information

        ucCalendarContainer _cal;
        DateTime _d;

        public frmAddAppointment(DateTime d, ucCalendarContainer Cal)
        {
            InitializeComponent();
            //default values to avoid NULL into DB
            _cal = Cal;
            _d = d;
            dtpSTime.Value = DateTime.Parse("6:00:00 AM");
            dtpETime.Value = DateTime.Parse("7:00:00 AM");
        }

        private void EnterAppointmentDayForm(string date, string starttime, string endtime, string description)
        {

            //TODO abstract connection
            SqlConnection conn = new SqlConnection(Utilities.DBConnection);
            conn.Open();

            //TODO abstract commands into stored procs
            SqlCommand sc = new SqlCommand("INSERT INTO tblCalendarMain ([Date], [StartTime], [EndTime], [Description])" +
            "VALUES (@Date, @StartTime, @Endtime, @Description)", conn);
            sc.Parameters.AddWithValue("@Date", date);
            sc.Parameters.AddWithValue("@StartTime", starttime);
            sc.Parameters.AddWithValue("@EndTime", endtime);
            sc.Parameters.AddWithValue("@Description", description);
            sc.ExecuteNonQuery();
            _cal.PaintNewPanel();
            conn.Close();
        }
        private void refillGrid(DateTime d)
        {
            //TODO abstract
            SqlConnection conn = new SqlConnection(Utilities.DBConnection);
            conn.Open();

            //TODO abstract
            SqlCommand sc = new SqlCommand("SELECT * FROM tblCalendarMain WHERE Date='" + d + "'", conn);
            sc.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(sc);
            DataTable ds = new DataTable();
            da.SelectCommand = sc;
            da.Fill(ds);
            //dataGridView1.DataSource = ds;
            //dataGridView1.Columns["pkID"].Visible = false;
            //dataGridView1.Columns["Date"].Visible = false;
            //dataGridView1.Columns["EndTime"].Visible = false;
            //dtpDate.Value = d;
            conn.Close();
            conn.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            
            if (MessageBox.Show("Are You Sure You Want To Commit This Appointment?", "Confirm" +
    " Commit", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string date;
                string starttime;
                string endtime;
                string description;

                //TODO this will probably change drastically when all done abstracting

                date = dtpDate.Value.ToString();
                starttime = dtpSTime.Value.ToString();
                endtime = dtpETime.Value.ToString();
                description = txtDesc.Text;

                EnterAppointmentDayForm(date, starttime, endtime, description);

                //this.dataGridView1.Invalidate();
                refillGrid(_d);
            }
            else
            {
                MessageBox.Show("Nothing Was Committed");
            }
        }
    }
}
